function binarySearch(arr, left, right, target) {
  if (left > right) {
    return -1;
  } else {
    let mid = left + (right - left) >> 1;
    if (arr[mid] === target) {
      return mid;
    } else if (arr[mid] < target) {
      return binarySearch(arr, mid + 1, right, target);
    } else {
      return binarySearch(arr, left, mid - 1, target);
    }
  }
}

let arr = [1, 3, 4, 5, 6, 8, 12, 14, 16];
console.log(binarySearch(arr, 0, 8, 8));